<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title> Sorting </title>

  <link rel="stylesheet" media="screen" href="/css/style.css" >
</head>
<body>

  <header class="site-header">
    <h3 class="site-title">
      <a href="https://mkaz.github.io/working-with-go/">
        Working with Go
      </a>
    </h3>
  </header>

  <main>

    <aside>
      <h2>Contents</h2>
      <ul>
        
          <li><a href="/hello.html">Hola Mundo</a></li>
        
          <li><a href="/strings.html">Strings</a></li>
        
          <li><a href="/slices.html">Slices</a></li>
        
          <li><a href="/functions.html">Functions</a></li>
        
          <li><a href="/control-structures.html">Control Structures</a></li>
        
          <li><a href="/for-loops.html">For Loops</a></li>
        
          <li><a href="/structs.html">Structs</a></li>
        
          <li><a href="/maps.html">Maps</a></li>
        
          <li><a href="/files.html">Files</a></li>
        
          <li><a href="/read-dir.html">Directories</a></li>
        
          <li><a href="/objects.html">Objects</a></li>
        
          <li><a href="/command-line.html">Command-line</a></li>
        
          <li><a href="/date-time.html">Date/Time</a></li>
        
          <li><a href="/sorting.html">Sorting</a></li>
        
          <li><a href="/json.html">JSON</a></li>
        
          <li><a href="/web-server.html">Web Server</a></li>
        
          <li><a href="/guess-game.html">Guessing Game</a></li>
        
          <li><a href="/rest-api.html">REST API</a></li>
        
          <li><a href="/regexps.html">Regexp</a></li>
        
          <li><a href="/memcache.html">Memcache</a></li>
        
          <li><a href="/mysql.html">MySQL</a></li>
        
          <li><a href="/interfaces.html">Interfaces</a></li>
        
          <li><a href="/testing.html">Testing</a></li>
        
      </ul>
    </aside>

    <article class="entry-content">
      <h1>Sorting Arrays and Maps</h1>

<p>The <a href="https://golang.org/pkg/sort/"><code>sort</code></a> standard library is used for sorting slices, maps, and user define collections.</p>

<h2>Sort Alphabetical</h2>

<p>Use <code>sort.Strings()</code> to sort alphabetically</p>

<pre><code class="language-go">abc := []string{&quot;jkl&quot;, &quot;ghi&quot;, &quot;abc&quot;, &quot;def&quot;}
sort.Strings(abc)
fmt.Println(&quot;Sorted ABC:&quot;, abc)
</code></pre>

<h2>Sort Numeric</h2>

<p>Use <code>sort.Ints()</code> to sort numerically</p>

<pre><code class="language-go">nums := []int{4, 2, 12, 5, 1, 3}
sort.Ints(nums)
fmt.Println(&quot;Sorted Nums:&quot;, nums)
</code></pre>

<h2>Reverse Sort</h2>

<p>Reverse sort, need to cast <code>abc</code> as a StringSlice to reverse and sort</p>

<pre><code class="language-go">sort.Sort(sort.Reverse(sort.StringSlice(abc)))
fmt.Println(&quot;Reverse ABC:&quot;, abc)
</code></pre>

<h2>Sort Maps</h2>

<p>To sort a map by keys, pull an array of keys out of the map, sort the keys and then iterate over the sorted keys. A map by itself is not sortable.</p>

<pre><code class="language-go">hash := map[string]int{
	&quot;c&quot;: 3,
	&quot;a&quot;: 1,
	&quot;b&quot;: 2,
	&quot;e&quot;: 5,
	&quot;d&quot;: 4,
}

// Create array of kys
var keys []string
for k := range hash {
	keys = append(keys, k)
}

// Sort keys
sort.Strings(keys)

// Use ordered keys to loop through hash
for i := range keys {
	fmt.Printf(&quot;%s =&gt; %v\n&quot;, keys[i], hash[keys[i]])
}
</code></pre>

    </article>

  </main>

<footer class="site-footer" role="contentinfo">
    An <a href="https://mkaz.blog/">mkaz</a> joint
    <span class="sep"> | </span>
    <a href="https://github.com/mkaz/hasite">Powered by Hastie</a>
</footer>

<script src="/js/asciinema/asciinema-player.js"></script>
<script src="/js/prism.js"></script>

</body>
</html>